﻿Imports Control_Vuelos_BL
Imports Control_Vuelos_BE
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraGrid.Views.Grid

Public Class frmAdm_Asiento
    Inherits frmBaseMantenimiento
    ReadOnly AsientoBE As New V_AsientoBE()
    ReadOnly vConciliaBL As New ConsolidadoBL()
    ReadOnly vMaestroBL As New MaestrosBL()
    Dim tbPeriodo As New DataTable
    Dim nopc As Integer = 0

    Private Sub frmAdm_Asiento_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        AddHandler BarBtnGrabar.ItemClick, AddressOf RegistroGrabar
        AddHandler UcAsiento1.btnBuscar.Click, AddressOf btnClickBuscarTicket
        AddHandler UcAsiento1.cboPeriodo.GotFocus, AddressOf pr_Cbo_GotFocus

        BarBtnAgregar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnModificar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnEliminar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnRefresh.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnImprimir.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnCancelar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        CargarCombos()
        SLTitulo.Text = "Generar Asiento"
        nopc = 0
    End Sub

    Private Sub RegistroGrabar()
        Dim row As DataRow = TryCast(UcAsiento1.GridView1.GetDataRow(UcAsiento1.GridView1.GetSelectedRows()(0)), DataRow)
        If Not row Is Nothing Then
            Dim snroticket As String = row("nroTicket")
            Dim snroPeriodo As String = row("nroPeriodo")
            Dim sCodAsiento As String = row("CodAsiento")
            Dim sNroAsiento As String = row("NroAsiento")
            ' Genero el asiento
            Dim sAsiento As String = vConciliaBL.GenerarAsiento(snroticket, snroPeriodo, sCodAsiento.Trim, sNroAsiento.Trim)
            ' cargo el numero de asiento en la grilla
            row("Asiento") = sAsiento
            row("NroAsiento") = Strings.Right(sAsiento, 4)
        End If
    End Sub

    Private Sub btnClickBuscarTicket()
        Dim sFiltro As String = String.Format("NroPeriodo = '{0}'", UcAsiento1.cboPeriodo.EditValue)
        Dim dtConsulta As DataTable = vConciliaBL.ListarTickets(sFiltro)
        If dtConsulta.Rows.Count > 0 Then
            BarBtnGrabar.Enabled = True
            UcAsiento1.GridControl1.DataSource = dtConsulta
            UcAsiento1.GridControl1.Focus()
        Else
            MsgBox("No hay tickets consolidados para el periodo requerido...", MsgBoxStyle.Information, Text)
        End If
    End Sub

    Private Sub pr_Cbo_GotFocus()
        Dim dt As New DataTable
        dt.Reset()
        UcAsiento1.GridControl1.DataSource = dt
        BarBtnGrabar.Enabled = False
    End Sub

    Private Sub CargarCombos()
        tbPeriodo = vMaestroBL.Get_Periodo()
        UcAsiento1.cboPeriodo.Properties.DataSource = tbPeriodo
        UcAsiento1.cboPeriodo.Properties.DisplayMember = "NomPeriodo"
        UcAsiento1.cboPeriodo.Properties.ValueMember = "NroPeriodo"
        UcAsiento1.cboPeriodo.Properties.Columns.Add(New DevExpress.XtraEditors.Controls.LookUpColumnInfo("NroPeriodo", 6, "Periodo"))
        UcAsiento1.cboPeriodo.Properties.Columns.Add(New DevExpress.XtraEditors.Controls.LookUpColumnInfo("NomPeriodo", 20, "Descripción"))
        UcAsiento1.cboPeriodo.Properties.BestFit()
        UcAsiento1.cboPeriodo.Properties.PopupWidth = 120
        UcAsiento1.cboPeriodo.Properties.SearchMode = SearchMode.AutoComplete
        UcAsiento1.cboPeriodo.Properties.AutoSearchColumnIndex = 1
    End Sub
End Class